<?php

/**
 * @author Samrat Khan & Sadik Sarfaraz - 10:22:43 AM Jul 31, 2013 
 */
require('top.inc.php');
if(isset($_SESSION['user']) && $_SESSION['user']['utype'] == 1 || $_SESSION['user']['utype'] == 2 || $_SESSION['user']['utype'] == 3 || $_SESSION['user']['utype'] == 4) {
    $sess_cusID = $_SESSION['user']['cus_id'];
    define('CSV_SEPARATOR', ',');

    // CSV download headers
    header('Content-Type: text/csv; charset=UTF-8');
    header('Content-Disposition: attachment; filename="my.csv"');

    // A file handle to PHP output stream
    $fp = fopen('php://output', 'w');

    // UTF-8 BOM
    echo "\xEF\xBB\xBF";

    // Get data from database
    $data = selectAll("ASC", "cus_id", "customerinfo", $db);

    // List of columns
    $columns = array(
      'User name',
      'First name',
      'Last name'
    );

    foreach ($data as $key => $row) {

      // Write columns
      if ($key == 0) {
        fputcsv($fp, $columns, CSV_SEPARATOR);
      }

      // Write data
      fputcsv($fp, $row, CSV_SEPARATOR);
    }

    fclose($fp);
    } else {
    header("Location: login.php");
}
?>
<?php
    include ('bottom.inc.php');
?>